package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.PermissionGroupMapper;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.PermissionGroup;
import com.hq.ims.data.entityenum.EnumGroupType;
import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.vo.PermissionGroupMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 角色或权限分类 服务实现类
 * @since 20240929
 */
@Service
public class BasePermissionGroupService extends BaseService<PermissionGroupMapper, PermissionGroup> {

    @Resource
    private BasePermissionRoleService basePermissionRoleService;
    @Resource
    private BasePermissionAuthorityService basePermissionAuthorityService;
    @Resource
    private BaseCompanyService baseCompanyService;

    /**
     * 分页
     */
    public List<PermissionGroupMapstruct.PermissionGroupVo> getPage(
            RequestPage page,
            PermissionGroupMapstruct.PageSearchVo searchVo
    ) {
        LambdaQueryChainWrapper<PermissionGroup> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(PermissionGroup::getCreateDateTime);

        //查询条件
        lq.eq(searchVo.getGroupType() != null, PermissionGroup::getGroupType, searchVo.getGroupType());
        lq.like(StringUtils.isNotEmpty(searchVo.getGroupName()), PermissionGroup::getGroupName, searchVo.getGroupName());

        //执行分页
        lq.page(page);

        List<PermissionGroup> dataList = page.getRecords();
        List<PermissionGroupMapstruct.PermissionGroupVo> voList = PermissionGroupMapstruct.INSTANCE.toPermissionAuthorityVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public PermissionGroup addData(
            PermissionGroupMapstruct.SaveVo vo
    ) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        BooleanUtils.isTrueThrow(this.isExistsGroupName(vo.getGroupName(), null), ErrMsgConstant.PERMISSION_GROUP_NAME_EXISTS);
        Company company = baseCompanyService.getById(vo.getCompanyId());
        ObjectUtils.isNullThrow(company, ErrMsgConstant.COMPANY_INFO_NOT_EXISTS);
        //账值新的对象
        PermissionGroup entity = new PermissionGroup();
        entity.setGroupName(vo.getGroupName());
        entity.setGroupType(vo.getGroupType());
        entity.setSortNumber(vo.getSortNumber());
        entity.setCompanyId(vo.getCompanyId());
        entity.setPermissionGroupId(vo.getPermissionGroupId());
        entity.setIsAllowEdit(true);
        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public PermissionGroup editData(PermissionGroupMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);
        BooleanUtils.isTrueThrow(this.isExistsGroupName(vo.getGroupName(), vo.getPermissionGroupId()), ErrMsgConstant.PERMISSION_GROUP_NAME_EXISTS);
        Company company = baseCompanyService.getById(vo.getCompanyId());
        ObjectUtils.isNullThrow(company, ErrMsgConstant.COMPANY_INFO_NOT_EXISTS);

        //账值新的对象
        PermissionGroup entity = this.getById(vo.getPermissionGroupId());
        ObjectUtils.isNullThrow(entity, ErrMsgConstant.PERMISSION_GROUP_NOT_EXISTS);

        entity.setGroupName(vo.getGroupName());
        entity.setGroupType(vo.getGroupType());
        entity.setSortNumber(vo.getSortNumber());
        entity.setCompanyId(vo.getCompanyId());
        entity.setPermissionGroupId(vo.getPermissionGroupId());
        entity.setIsAllowEdit(true);

        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String permissionGroupId) {
        PermissionGroup data = this.getById(permissionGroupId);
        if (data == null) {
            return;
        }
        boolean bl = false;
        if (data.getGroupType() == EnumGroupType.Auth) {
            bl = basePermissionAuthorityService.groupDataIsExists(permissionGroupId);
        } else if (data.getGroupType() == EnumGroupType.Role) {
            bl = basePermissionRoleService.groupDataIsExists(permissionGroupId);
        }
        BooleanUtils.isTrueThrow(bl, ErrMsgConstant.PERMISSION_GROUP_HAVE_CHILDREN_DATA_NOT_DEL);
        data.deleteById();
    }

    /**
     * 组名称是否存在
     *
     * @param groupName                组名称
     * @param excludePermissionGroupId 需要排除的ID
     */
    public boolean isExistsGroupName(
            String groupName,
            String excludePermissionGroupId
    ) {
        return this.lambdaQuery().eq(PermissionGroup::getGroupName, groupName)
                .ne(StringUtils.isNotEmpty(excludePermissionGroupId), PermissionGroup::getPermissionGroupId, excludePermissionGroupId)
                .count() > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyId) {
        this.lambdaUpdate().eq(PermissionGroup::getCompanyId,companyId).remove();
    }
}